package com.pinusmusic.web.serlvet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pinusmusic.domain.*;
import com.pinusmusic.service.AdminService;
import com.pinusmusic.service.impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/admin/*")
public class AdminServlet extends BaseServlet {
    private AdminService as = new AdminServiceImpl();
    //管理员登陆
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String adminname = req.getParameter("adminname");
        String adminpassword = req.getParameter("adminpassword");
        boolean flag = as.findAdminByNameAndPassword(adminname,adminpassword);
        ResultInfo info = new ResultInfo();
        if (flag){
            req.getSession().setAttribute("adminname",adminname);
            info.setFlag(true);
        }else {
            info.setFlag(false);
            info.setErrorMsg("管理员用户名或者密码错误！");
        }
        writeValue(resp,info);
    }
    //查询所有用户
    public void findAllUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list = as.findAllUser();
        writeValue(resp,list);
    }
    //管理员删除用户
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _uid = req.getParameter("uid");
        int uid = Integer.parseInt(_uid);
        List<Favorite> list = as.findFavByUid(uid);
        if(list!=null){
            as.deleteFavByUid(uid);
        }
        as.deleteUserByUid(uid);
//        as.deleteComByUid(uid);
    }
    //管理员修改评论权限
    public void updatecomment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        as.updateComment(Integer.parseInt(uid));
    }

    public void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageSize = 8;
        String curStr = req.getParameter("currentPage");
        if (curStr==null || curStr.equals("") || curStr.equals("0")) curStr="1";
        int currentPage = Integer.parseInt(curStr);
        PageBean<User> pb = as.findUserByPage(pageSize,currentPage);
        writeValue(resp,pb);
    }
    public void deleteUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uids = req.getParameter("uids");
        List<Favorite> list = as.findFavByUid(Integer.parseInt(uids));
        if(list!=null){
            as.deleteFavByUid(Integer.parseInt(uids));
        }
        as.deleteUserByUid(Integer.parseInt(uids));
//        as.deleteComByUid(Integer.parseInt(uids));
    }
    public void updatecomments(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uids = req.getParameter("uids");
        as.updateComment(Integer.parseInt(uids));
    }
    public void open(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uids = req.getParameter("uids");
        as.open(Integer.parseInt(uids));
    }
    public void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.销毁session域中的user
        req.getSession().removeAttribute("adminname");
        //2.跳转到新的页面 - login.html
        resp.sendRedirect(req.getContextPath() + "/userhtml/index.html");
    }
    public void getusername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String adminname = (String)req.getSession().getAttribute("adminname");
        Admin admin = new Admin();
        admin.setAdminname(adminname);
        if (adminname!=null){
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(admin);
            resp.setContentType("application/json;charset-utf-8");
            resp.getWriter().write(json);
        }
    }
}
